package com.wx.mall.operate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wx.mall.operate.dto.RebLinkDto;
import com.wx.mall.operate.entity.RebLink;
import com.wx.mall.operate.entity.RebSort;
import com.wx.mall.operate.mapper.RebLinkMapper;
import com.wx.mall.operate.service.IRebLinkService;
import com.wx.mall.operate.service.IRebSortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author z
 * @since 2020-6-4
 */
@Service
public class RebLinkServiceImpl extends ServiceImpl<RebLinkMapper, RebLink> implements IRebLinkService {

    @Autowired
    IRebSortService iRebSortService;

    @Autowired
    IRebLinkService iRebLinkService;

    @Override
    public List<Long> getFindSort(Long ids) {
        QueryWrapper<RebLink> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("rebate_id",ids);
        List<RebLink> links=this.list(queryWrapper);
        List<Long> list=new ArrayList<>();
        for (RebLink reblink:links
             ) {
            RebSort byId = this.findById(reblink.getSortId());
            if (byId.getType()==2){
                list.add(reblink.getSortId());
            }
        }
        return list;
    }

    @Override
    public int getSortLink(RebLinkDto dto) {
        QueryWrapper<RebLink> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("rebate_id",dto.getRebateId());
        List<RebLink> list = this.list(queryWrapper);
        int mma=1;
        if (list.size()==0){
            for (Long i:dto.getId()
            ) {
                RebLink rebLink=new RebLink();
                rebLink.setRebateId(dto.getRebateId());
                rebLink.setSortId(i);
                boolean save = iRebLinkService.save(rebLink);
                if (save==true){
                    mma=1;
                }else {
                    mma=0;
                }
            }
        }else {
            int aa=1;
            for (RebLink ra:list
            ) {
                boolean b = iRebLinkService.removeById(ra.getId());
                if (b==true){
                    aa=1;
                }else {
                    aa=0;
                }
            }
                if (aa==1){
                    for (Long a:dto.getId()
                    ) {
                        RebLink rebLink=new RebLink();
                        rebLink.setRebateId(dto.getRebateId());
                        rebLink.setSortId(a);
                        boolean save1 = iRebLinkService.save(rebLink);
                        if (save1==true){
                            mma=1;
                        }else {
                            mma=0;
                        }
                    }
                }
        }
        return mma;
    }

    @Override
    public int deleteSort(Long ids) {
        QueryWrapper<RebLink> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("rebate_id",ids);
        List<RebLink> list = this.list(queryWrapper);
        int ll=1;
        for (RebLink rr:list
             ) {
            boolean b = iRebLinkService.removeById(rr.getId());
            if (b==true){
                ll=1;
            }else {
                ll=0;
            }
        }
        return ll;
    }

    private RebSort findById(Long id){
        RebSort byId = iRebSortService.getById(id);
        return byId;
    }
}
